package com.sub.project.system.controller;

import java.util.List;
import java.util.Set;

import com.sub.framework.redis.RedisCache;
import com.sub.project.info.domain.*;
import com.sub.project.info.service.*;
import com.sub.project.info.vo.EducationSchoolVo;
import com.sub.project.system.domain.AppletLoginRequset;
import com.sub.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.sub.common.constant.Constants;
import com.sub.common.utils.ServletUtils;
import com.sub.framework.security.LoginUser;
import com.sub.framework.security.service.SysLoginService;
import com.sub.framework.security.service.SysPermissionService;
import com.sub.framework.security.service.TokenService;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.project.system.domain.SysMenu;
import com.sub.project.system.domain.SysUser;
import com.sub.project.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;
    /** 个人荣誉*/
    @Autowired
    private IHonorService iHonorService;
    /** 个人的教育经历*/
    @Autowired
    private IUserEducationalService educationalService;
    /** 个人技能*/
    @Autowired
    private IUserTechnologyService iUserTechnologyService;
    @Autowired
    private ISysProjectService projectService;
    @Autowired
    private IWorkExperienceService workExperienceService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 登录方法
     * 
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(String username, String password, String code, String uuid)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(username, password, code, uuid);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/applet/login")
    public AjaxResult applatLogin(@RequestBody AppletLoginRequset requset)
    {
        return loginService.appletLogin(requset.getCode(),requset.getAvatarUrl(),requset.getNickName());
    }
    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @GetMapping("appletGetInfo")
    public AjaxResult appletGetInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        AjaxResult ajaxResult = redisCache.getCacheObject("RESUME:INFO"+user.getOpenId());
        if (ajaxResult == null){

            /** 获取个人简历信息*/
            // 个人经验集合
            List<Honor>  honor =  iHonorService.findByopenId(user.getUserName());
            /** 个人项目 */
            List<SysProject> projects = projectService.findByopenId(user.getUserName());
            /** 个人的教育经历*/
            List<EducationSchoolVo> educationals = educationalService.findByopenId(user.getUserName());
            /** 个人技能*/
            List<UserTechnology> technologies = iUserTechnologyService.findByopenId(user.getUserName());
            /** 个人工作经验*/
            List<WorkExperience> work = workExperienceService.findByopenId(user.getUserName());
            // 权限集合
            AjaxResult ajax = AjaxResult.success();
            ajax.put("user", user);
            /*个人荣誉*/
            ajax.put("honor", honor);
            /** 专业技能*/
            ajax.put("techology", technologies);
            /** 工作经历*/
            ajax.put("work", work);
            /** 教育经历*/
            ajax.put("educat", educationals);
            /** 项目经验*/
            ajax.put("project", projects);
            redisCache.setCacheObject("RESUME:INFO"+user.getOpenId(),ajax);
            return  ajax;
       }else {
            return ajaxResult;
        }
    }
    @GetMapping("/passToken/appletGetInfo")
    public  AjaxResult passToken(){

        AjaxResult ajaxResult = redisCache.getCacheObject("RESUME:INFO:osP3-0P9pJwc-d8v5fGI74TVcjV8");
        if (ajaxResult == null){
            /** 获取个人简历信息*/
            // 个人经验集合
            SysUser user= sysUserService.selectUserByOpenId("osP3-0P9pJwc-d8v5fGI74TVcjV8");
            List<Honor>  honor =  iHonorService.findByopenId(user.getUserName());
            /** 个人项目 */
            List<SysProject> projects = projectService.findByopenId(user.getUserName());
            /** 个人的教育经历*/
            List<EducationSchoolVo> educationals = educationalService.findByopenId(user.getUserName());
            /** 个人技能*/
            List<UserTechnology> technologies = iUserTechnologyService.findByopenId(user.getUserName());
            /** 个人工作经验*/
            List<WorkExperience> work = workExperienceService.findByopenId(user.getUserName());
            // 权限集合
            AjaxResult ajax = AjaxResult.success();
            ajax.put("user", user);
            /*个人荣誉*/
            ajax.put("honor", honor);
            /** 专业技能*/
            ajax.put("techology", technologies);
            /** 工作经历*/
            ajax.put("work", work);
            /** 教育经历*/
            ajax.put("educat", educationals);
            /** 项目经验*/
            ajax.put("project", projects);
            redisCache.setCacheObject("RESUME:INFO:osP3-0P9pJwc-d8v5fGI74TVcjV8",ajax);
            return  ajax;
        }else {
            return ajaxResult;
        }
    }


    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     *  获取基础用户信息。
     * @return
     */
    @GetMapping("wechatGetInfo")
    public  AjaxResult getWechat(){
        return  new AjaxResult();
    };
}
